<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Vue 面试题 | Justic</title><meta name="keywords" content="Vue"><meta name="author" content="Justic"><meta name="copyright" content="Justic"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="一、Vue 面试基础知识嘟嘟嘟 1、computed 和 watch computed有缓存，如果数据data没有发生变化的话，则不会重新计算。 watch如何实现深度的监听？ watch监听引用类型，获取不到oldValue  下面看一下watch的伪代码 export default &amp;#123;  data() &amp;#123;    return &amp;#123;      name: &amp;quot">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue 面试题">
<meta property="og:url" content="http://example.com/2022/07/15/Vue/Vue%20%E5%B8%B8%E8%A7%81%E9%9D%A2%E8%AF%95%E9%A2%98/Vue%E9%9D%A2%E8%AF%95%E9%A2%98/index.html">
<meta property="og:site_name" content="Justic">
<meta property="og:description" content="一、Vue 面试基础知识嘟嘟嘟 1、computed 和 watch computed有缓存，如果数据data没有发生变化的话，则不会重新计算。 watch如何实现深度的监听？ watch监听引用类型，获取不到oldValue  下面看一下watch的伪代码 export default &amp;#123;  data() &amp;#123;    return &amp;#123;      name: &amp;quot">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/yj/wallhaven-yj1kgl.jpg">
<meta property="article:published_time" content="2022-07-15T12:05:37.000Z">
<meta property="article:modified_time" content="2022-07-22T12:42:47.356Z">
<meta property="article:author" content="Justic">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/yj/wallhaven-yj1kgl.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2022/07/15/Vue/Vue%20%E5%B8%B8%E8%A7%81%E9%9D%A2%E8%AF%95%E9%A2%98/Vue%E9%9D%A2%E8%AF%95%E9%A2%98/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Vue 面试题',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-07-22 20:42:47'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-clock-anzhiyu/lib/clock.min.css" /><!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.2.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="https://portrait.gitee.com/uploads/avatars/user/2982/8948190_JIAXInT_1624177607.png!avatar200" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">22</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">2</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/project/"><i class="fa-fw fas fa-images"></i><span> 项目</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于笔者</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/yj/wallhaven-yj1kgl.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Justic</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/project/"><i class="fa-fw fas fa-images"></i><span> 项目</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于笔者</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Vue 面试题</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-07-15T12:05:37.000Z" title="发表于 2022-07-15 20:05:37">2022-07-15</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-07-22T12:42:47.356Z" title="更新于 2022-07-22 20:42:47">2022-07-22</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Vue/">Vue</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">5.7k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>21分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Vue 面试题"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span><span class="post-meta-separator">|</span><span class="post-meta-commentcount"><i class="far fa-comments fa-fw post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/2022/07/15/Vue/Vue%20%E5%B8%B8%E8%A7%81%E9%9D%A2%E8%AF%95%E9%A2%98/Vue%E9%9D%A2%E8%AF%95%E9%A2%98/#post-comment" itemprop="discussionUrl"><span class="valine-comment-count" data-xid="/2022/07/15/Vue/Vue%20%E5%B8%B8%E8%A7%81%E9%9D%A2%E8%AF%95%E9%A2%98/Vue%E9%9D%A2%E8%AF%95%E9%A2%98/" itemprop="commentCount"><i class="fa-solid fa-spinner fa-spin"></i></span></a></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="一、Vue-面试基础知识"><a href="#一、Vue-面试基础知识" class="headerlink" title="一、Vue 面试基础知识"></a>一、Vue 面试基础知识</h1><p>嘟嘟嘟</p>
<h2 id="1、computed-和-watch"><a href="#1、computed-和-watch" class="headerlink" title="1、computed 和 watch"></a>1、computed 和 watch</h2><ul>
<li><code>computed</code>有缓存，如果数据<code>data</code>没有发生变化的话，则不会重新计算。</li>
<li><code>watch</code>如何实现深度的监听？</li>
<li><code>watch</code>监听引用类型，获取不到<code>oldValue</code></li>
</ul>
<p>下面看一下<code>watch</code>的伪代码</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="title function_">data</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="attr">name</span>: <span class="string">&quot;zhangsan&quot;</span>,</span><br><span class="line">      <span class="attr">info</span>: &#123;</span><br><span class="line">        <span class="attr">city</span>: <span class="string">&quot;上海&quot;</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">watch</span>: &#123;</span><br><span class="line">    <span class="title function_">name</span>(<span class="params">oldVal, val</span>) &#123;</span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(oldVal, val); <span class="comment">//值类型，可以正常获取到值</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">info</span>: &#123;</span><br><span class="line">      <span class="title function_">handler</span>(<span class="params">oldVal, val</span>) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(oldVal, val); <span class="comment">//引用类型，获取不到oldVal,因为指针相同，此时已经指向了新的val</span></span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="attr">deep</span>: <span class="literal">true</span>, <span class="comment">//深度监听</span></span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>这里 还需要注意<code>watch</code> 的一个特点是，最初绑定的时候是不会执行的，要等到 <code>firstName</code> 改变时才执行监听计算。那我们想要一开始就让他最初绑定的时候就执行改怎么办呢？</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="attr">watch</span>: &#123;</span><br><span class="line">  <span class="attr">firstName</span>: &#123;</span><br><span class="line">    <span class="title function_">handler</span>(<span class="params">newName, oldName</span>) &#123;</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">fullName</span> = newName + <span class="string">&#x27; &#x27;</span> + <span class="variable language_">this</span>.<span class="property">lastName</span>;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 代表在wacth里声明了firstName这个方法之后立即先去执行handler方法</span></span><br><span class="line">    <span class="attr">immediate</span>: <span class="literal">true</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>而<code>immediate:true</code>代表如果在 <code>wacth </code>里声明了 <code>firstName </code>之后，就会立即先去执行里面的<code>handler</code>方法，如果为 <code>false</code>就跟我们以前的效果一样，不会在绑定的时候就执行。</p>
<h2 id="2、v-if-与-v-show"><a href="#2、v-if-与-v-show" class="headerlink" title="2、v-if 与 v-show"></a>2、v-if 与 v-show</h2><p>简单的说一下，它两者的区别：如果条件满足，使用<code>v-if</code>与<code>v-show</code>都会创建对应的元素并展示出对应的内容，如果条件不满足<code>v-else</code>的内容不会展示也就是不会创建对应的元素，而<code>v-show</code>会创建出对应的元素，但是不会展示，使用<code>style=&quot;display:none&quot;</code>将其隐藏掉。</p>
<p>两者如何选择呢？如果元素的创建不是很频繁，可以使用<code>v-if与v-else</code>,如果某些元素需要频繁的创建或者是需要不断的切换元素的显示隐藏状态，就需要使用<code>v-show</code>,这样性能会比较好，而如果使用<code>v-if</code>与<code>v-else</code>就需要不断创建与销毁元素，这样性能比较低了。</p>
<h2 id="3、循环列表"><a href="#3、循环列表" class="headerlink" title="3、循环列表"></a>3、循环列表</h2><ul>
<li><code>key</code>的重要性，注意<code>key</code>不能乱写，例如不能使用<code>random</code>或者是<code>index</code></li>
<li><code>v-for</code>和<code>v-if</code>不能一起使用！</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;ul&gt;</span><br><span class="line">    &lt;li v-if=&quot;flag&quot; v-for=&quot;item in lists&quot; :key=&quot;item.id&quot;&gt;&lt;/li&gt;</span><br><span class="line">    </span><br><span class="line">&lt;/ul&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      flag: false,</span><br><span class="line">      lists: [],</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p><code>v-for</code>的优先级要高于<code>v-if</code>,这样的话就会先去执行循环，这样数组中的内容都循环完毕了，才会使用<code>v-if</code>进行判断。这样判断会被执行多次，而且是重复的判断。</p>
<h2 id="4、父子组件如何通信"><a href="#4、父子组件如何通信" class="headerlink" title="4、父子组件如何通信"></a>4、父子组件如何通信</h2><p>父子组件的通信通过<code>props</code>与<code>$emit</code>完成，关于这块内容在前面的课程中已经重点讲解过了，这里不在进行讲解了。</p>
<h2 id="5、兄弟组件之间的通信"><a href="#5、兄弟组件之间的通信" class="headerlink" title="5、兄弟组件之间的通信"></a>5、兄弟组件之间的通信</h2><p>自定义事件的实现方式</p>
<p>这里需要注意的一点就是最后要及时销毁自定义的事件，否则可能会造成内存泄漏的问题.</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="title function_">beforeDestroy</span>(<span class="params"></span>)&#123;</span><br><span class="line">    event.$off(<span class="string">&#x27;onAddSub&#x27;</span>,<span class="variable language_">this</span>.<span class="property">addSub</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="6、生命周期"><a href="#6、生命周期" class="headerlink" title="6、生命周期"></a>6、生命周期</h2><p>生命周期分为哪几个阶段？</p>
<ul>
<li>挂载阶段</li>
<li>更新阶段</li>
<li>销毁阶段</li>
</ul>
<p>重点理解官网中的图</p>
<h2 id="7、自定义-v-model"><a href="#7、自定义-v-model" class="headerlink" title="7、自定义 v-model"></a>7、自定义 v-model</h2><p><code>v-model</code>是<code>vue</code>实现数据双向绑定最好的一个指令, <code>v-model</code>本质上不过是语法糖,它负责监听用户的输入事件以更新数据,当你修改页面的时候 <code>v-model </code>自动去更新数据层 (<code>model)</code>,当你修改数据的时候 v-model 自动去更新视图层 (<code>view</code>)</p>
<h2 id="8、-nextTick"><a href="#8、-nextTick" class="headerlink" title="8、$nextTick"></a>8、$nextTick</h2><ul>
<li><code>Vue</code>是异步渲染</li>
<li><code>data</code>改变之后，<code>DOM</code>不会立刻渲染</li>
<li><code>$nextTick</code>会在<code>DOM</code>渲染之后被触发，以获取最新的<code>DOM</code>节点。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div id=&quot;app&quot;&gt;</span><br><span class="line">    &lt;ul ref=&quot;ul1&quot;&gt;</span><br><span class="line">      &lt;li v-for=&quot;(item, index) in list&quot; :key=&quot;index&quot;&gt;</span><br><span class="line">        &#123;&#123; item &#125;&#125;</span><br><span class="line">      &lt;/li&gt;</span><br><span class="line">    &lt;/ul&gt;</span><br><span class="line">    &lt;button @click=&quot;addItem&quot;&gt;添加一项&lt;/button&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  name: &quot;app&quot;,</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      list: [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;],</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    addItem() &#123;</span><br><span class="line">      this.list.push(`$&#123;Date.now()&#125;`);</span><br><span class="line">      this.list.push(`$&#123;Date.now()&#125;`);</span><br><span class="line">      this.list.push(`$&#123;Date.now()&#125;`);</span><br><span class="line">      //获取dom元素</span><br><span class="line">      const ulElem = this.$refs.ul1;</span><br><span class="line">      console.log(ulElem.childNodes.length);</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p>如果我们点击了<code>按钮</code>以后，输出的子元素的个数是多上呢？这里是 3，可能你认为是<code>6</code>,因为在按钮所对应的处理函数中，我们向<code>list</code>数组中又添加了 3 项内容，这样<code>ul</code>中的子元素应该是 6(<code>list</code>是响应式的，数据发生变化后，重新渲染视图). 但是实际的输出结果为<code>3</code>.</p>
<p>原因是：当<code>data</code>中<code>list</code>数组中的内容发生了变化后，<code>DOM</code>元素并不会立即改变。这时获取到的<code>DOM</code>元素的内容还是以前的内容。</p>
<p>但是，这里我们希望当单击完按钮后，立即获取到最新的<code>DOM</code>元素内容，应该怎样处理呢？</p>
<p>修改<code>addItem</code>方法中的代码如下：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="title function_">addItem</span>(<span class="params"></span>)&#123;</span><br><span class="line">              <span class="variable language_">this</span>.<span class="property">list</span>.<span class="title function_">push</span>(<span class="string">`<span class="subst">$&#123;<span class="built_in">Date</span>.now()&#125;</span>`</span>)</span><br><span class="line">               <span class="variable language_">this</span>.<span class="property">list</span>.<span class="title function_">push</span>(<span class="string">`<span class="subst">$&#123;<span class="built_in">Date</span>.now()&#125;</span>`</span>)</span><br><span class="line">               <span class="variable language_">this</span>.<span class="property">list</span>.<span class="title function_">push</span>(<span class="string">`<span class="subst">$&#123;<span class="built_in">Date</span>.now()&#125;</span>`</span>)</span><br><span class="line">    			<span class="variable language_">this</span>.$nextTick(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">                    <span class="comment">//获取dom元素</span></span><br><span class="line">              <span class="keyword">const</span> ulElem=<span class="variable language_">this</span>.<span class="property">$refs</span>.<span class="property">url1</span></span><br><span class="line">              <span class="variable language_">console</span>.<span class="title function_">log</span>(ulElem.<span class="property">childNodes</span>.<span class="property">length</span>)</span><br><span class="line">              &#125;)</span><br><span class="line"></span><br><span class="line">          &#125;</span><br></pre></td></tr></table></figure>

<p>把获取<code>DOM</code>元素的内容放到<code>$nextTick</code>方法对应的回调函数内部就可以了。</p>
<p>总结：第一：<code>Vue</code>是异步渲染，<code>$nextTick</code>是在<code>DOM</code>渲染完以后才执行的。</p>
<p>第二：页面渲染时会将<code>data</code>的修改做整合，也就是说多次<code>data</code>修改只会渲染一次，例如上面的案例中，我们对<code>list</code>数组做了三次的修改，但是最终只做了一次渲染。这样<code>$nextTick</code>也就只调用了一次。</p>
<h2 id="9、slot"><a href="#9、slot" class="headerlink" title="9、slot"></a>9、slot</h2><p>基本使用</p>
<p>作用域插槽</p>
<p>具名插槽</p>
<h2 id="10、动态组件"><a href="#10、动态组件" class="headerlink" title="10、动态组件"></a>10、动态组件</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">:is=<span class="string">&quot;component-name&quot;</span></span><br></pre></td></tr></table></figure>

<p>什么时候用：需要根据数据，动态渲染的场景，即组件类型不确定。</p>
<p><img src= "" data-lazy-src="/images/%E5%8A%A8%E6%80%81%E7%BB%84%E4%BB%B6.png"></p>
<p>在上图中，展示的是一个新闻的详情页面，在这个页面中，最开始展示了一个文本组件，然后下面紧跟着展示了一个图像组件，最后再展示文本组件。</p>
<p>但是，我们知道不同的详情页面可能展示的组件的顺序是不一样的，也就是说，另外一个详情页面，可能最开始展示的是图像组件，然后才是文本组件，所以到底先展示什么是不确定的，有具体的数据来决定。</p>
<p>下面看一下动态组件的基本使用</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;div&gt;</span><br><span class="line">    &lt;!--动态组件--&gt;</span><br><span class="line">    &lt;component :is=&quot;NextTickName&quot;/&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">import NextTick from &quot;./NextTick&quot;;</span><br><span class="line">export default &#123;</span><br><span class="line">  components: &#123;</span><br><span class="line">    NextTick,</span><br><span class="line">  &#125;,</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      name: &quot;zhangsan&quot;,</span><br><span class="line">      obj: &#123;</span><br><span class="line">        username: &quot;zs&quot;,</span><br><span class="line">        userpwd: &quot;123&quot;,</span><br><span class="line">      &#125;,</span><br><span class="line">      NextTickName: &quot;NextTick&quot;,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p>现在，了解了动态组件的基本使用以后，下面我们通过一段伪代码，将上图的功能模拟一下。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;div&gt;</span><br><span class="line">    &lt;!--动态组件--&gt;</span><br><span class="line">     &lt;div v-for=&quot;item in newsData&quot; :key=&quot;item.id&quot;&gt;</span><br><span class="line">         &lt;component :is=&quot;item.type&quot;&gt;&lt;/component&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">	import NextTick from &#x27;./NextTick&#x27;</span><br><span class="line">    export default&#123;</span><br><span class="line">        components:&#123;</span><br><span class="line">            NextTick</span><br><span class="line">        &#125;,</span><br><span class="line">        data()&#123;</span><br><span class="line">            return&#123;</span><br><span class="line">                name:&#x27;zhangsan&#x27;,</span><br><span class="line">                obj:&#123;</span><br><span class="line">                    username:&#x27;zs&#x27;,</span><br><span class="line">                    userpwd:&#x27;123&#x27;</span><br><span class="line">                &#125;,</span><br><span class="line">               newData:[</span><br><span class="line">                   //type:表示对应的组件</span><br><span class="line">                   &#123;id:1,type:&#x27;text&#x27;&#125;</span><br><span class="line">                   &#123;id:2,type:&#x27;text&#x27;&#125;</span><br><span class="line">               	   &#123;id:3,type:&#x27;imgage&#x27;&#125;</span><br><span class="line">               ]</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<h2 id="11、异步加载组件"><a href="#11、异步加载组件" class="headerlink" title="11、异步加载组件"></a>11、异步加载组件</h2><p>在开发的时候，我们经常会加载一些体积比较大的组件，例如：编辑器，<code>echarts</code>等，</p>
<p>而这些组件的加载非常消耗性能，为了提高性能，可以采用异步的方式来进行加载。</p>
<p>·<code>import( )</code>函数实现按需加载，异步加载大组件</p>
<p>下面看一下<code>import</code>函数的使用。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;div&gt;</span><br><span class="line">    &lt;!--异步组件--&gt;</span><br><span class="line">    &lt;FormDemo v-if=&quot;showFormDemo&quot; /&gt;</span><br><span class="line">    &lt;button @click=&quot;showFormDemo=true&quot;&gt;show&lt;/button&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">//import FormDemo from &#x27;./component/FormDemo&#x27; //同步加载组件</span><br><span class="line">   export default &#123;</span><br><span class="line">       components:&#123;</span><br><span class="line">           //FormDemo</span><br><span class="line">           FormDemo:()=&gt;import(&#x27;./component/FormDemo&#x27;)</span><br><span class="line">       &#125;，</span><br><span class="line">       data()&#123;</span><br><span class="line">           return &#123;</span><br><span class="line">               showFormDemo:false</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p>通过上面的代码，我们可以看到最开始的时候是不会加载<code>FormDemo</code>这个组件的，当单击了<code>show</code>按钮的时候才会加载该组件，但是在加载该组件的时候是通过<code>import</code>方法来完成的，这就是异步加载组件，或者是按需加载，以前的加载方式是不管组件是否会用到都要进行加载，这其实就是同步的一种加载方式，这种方式会影响性能。</p>
<h2 id="12、keep-alive"><a href="#12、keep-alive" class="headerlink" title="12、keep-alive"></a>12、keep-alive</h2><p><code>keep-alive</code>实现缓存组件</p>
<p>什么时候会用到缓存组件呢？</p>
<p>需要进行频繁切换，不需要重复渲染的情况下，可以考虑使用<code>keep-alive</code>,例如 <code>Tab</code>页签组件</p>
<p>这也是常见的一种优化的手段。</p>
<h2 id="13、mixin"><a href="#13、mixin" class="headerlink" title="13、mixin"></a>13、mixin</h2><p><code>mixin</code>（混入）可以把多个组件中有相同的逻辑的内容，抽离出来。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;p&gt;&#123;&#123; name &#125;&#125; &#123;&#123; major &#125;&#125; &#123;&#123; city &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;button @click=&quot;showName&quot;&gt;显示姓名&lt;/button&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">import myMixin from &#x27;./mixin&#x27;</span><br><span class="line">    export default &#123;</span><br><span class="line">        mixins:[myMixin],//可以添加多个，会自动的进行合并</span><br><span class="line">        data()&#123;</span><br><span class="line">            return&#123;</span><br><span class="line">                name:&#x27;zhangsan&#x27;,</span><br><span class="line">                major:&#x27;web 前端&#x27;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;，</span><br><span class="line">        methods:&#123;&#125;,</span><br><span class="line">        mounted()&#123;</span><br><span class="line">         console.log(&#x27;componet name&#x27;,this.name)</span><br><span class="line">    	&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p>在上面的代码中，使用了<code>city</code>与<code>showName</code>,这两项内容并没有在当前组件中进行定义，而是在<code>mixin.js</code>文件中进行定义的，这里进行了混入，那么这样当前组件就有了这些内容。而<code>mixin.js</code>文件中实现的内容都是其他组件相同的逻辑的内容。</p>
<p><code>mixin.js</code>文件中的代码如下：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="title function_">data</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="attr">city</span>: <span class="string">&quot;北京&quot;</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">methods</span>: &#123;</span><br><span class="line">    <span class="title function_">showName</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">name</span>);</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="title function_">mounted</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;mixin mounted&quot;</span>, <span class="variable language_">this</span>.<span class="property">name</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>通过这个案例，我们可以看到，如果有很多组件，而且这些组件都有相同的逻辑，那么可以将这些相同的逻辑通过混入的方式引入到组件中，而在组件中定义的都是自己独有的内容。</p>
<p>当然，<code>mixin</code>也有自己的一些问题，</p>
<p>第一：变量来源不明确，不利于阅读，例如如下代码</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;p&gt;&#123;&#123;name&#125;&#125; &#123;&#123;major&#125;&#125; &#123;&#123;city&#125;&#125;&lt;/p&gt;</span><br></pre></td></tr></table></figure>

<p>假如当前组件混入了多项内容，那么在当前组件中我们使用了<code>ciity</code>变量，而该变量到底来自哪，不容易被查找。</p>
<p>第二：多<code>mixin</code>可能会造成命名冲突</p>
<p>第三点：<code>mixin</code>和组件可能出现多对多的关系，复杂度较高。</p>
<p>例如：一个组件可以引用多个<code>mixin</code>,而一个<code>mixin</code>可以被多个组件所引用，这样复杂度较高以后，很容易出现错误。</p>
<p>当然为了解决<code>mixin</code>的这些问题，在<code>Vue3</code>中提出了<code>Composition API</code>来进行解决。</p>
<h1 id="二、Vue-原理"><a href="#二、Vue-原理" class="headerlink" title="二、Vue 原理"></a>二、Vue 原理</h1><h2 id="1、MVVM"><a href="#1、MVVM" class="headerlink" title="1、MVVM"></a>1、MVVM</h2><p>传统组件，只是静态渲染，更新还需要依赖于<code>DOM</code>操作。</p>
<p>数据驱动–<code>Vue.js</code></p>
<p>所谓的数据驱动的理念：当数据发生变化的时候，用户界面也会发生相应的变化，开发者并不需要手动的去修改<code>dom</code>.</p>
<p>这样我们在开发的时候，更加关注的是数据（业务逻辑），而不是<code>dom</code>操作。</p>
<p><img src= "" data-lazy-src="/images/2.png"></p>
<p><code>MVVM</code> 框架主要包含三部分：<code>Model</code>,<code>View</code>,<code>ViewMode</code></p>
<p><code>Model</code>:指的是数据部分，对应到前端就是<code>JavaScript</code>对象。</p>
<p><code>View</code>:指的就是视图部分</p>
<p><code>ViewModel</code>: 就是连接视图与数据的中间件(中间桥梁)</p>
<p>数据(<code>Model</code>)和视图(<code>View</code>)是不能直接通讯的，而是需要通过<code>ViewModel</code>来实现双方的通讯。当数据(<code>Model</code>)变化的时候，<code>ViewModel</code>能够监听到这种变化，并及时通知<code>View</code>视图做出修改。同样的，当页面有事件触发的时候，<code>ViewModel</code>也能够监听到事件，并通知数据(<code>Model</code>)进行响应。所以<code>ViewModel</code>就相当于一个观察者，监控着双方的动作，并及时通知对方进行相应的操作。</p>
<p>简单的理解就是：<code>MVVM</code> 实现了将业务(数据)与视图进行分离的功能。</p>
<p>在这里还需要注意的一点就是：</p>
<p><code>MVVM</code>框架的三要素：响应式，模板引擎，渲染</p>
<p>响应式：<code>vue</code>如何监听数据的变化？</p>
<p>模板：<code>Vue</code>的模板如何编写和解析？怎样将具体的值替换掉<code>&#123;&#123;msg&#125;&#125;</code>内容，这就是模板引擎的解析。</p>
<p>渲染：<code>Vue</code>如何将模板转换成<code>html</code>? 其实就是有虚拟<code>DOM</code> 向真实<code>DOM</code>的转换。</p>
<h2 id="2、监听-data-变化的核心-API-是什么"><a href="#2、监听-data-变化的核心-API-是什么" class="headerlink" title="2、监听 data 变化的核心 API 是什么"></a>2、监听 data 变化的核心 API 是什么</h2><p>响应式就是指：组件<code>data</code>的数据一旦发生变化，立刻触发视图的更新。</p>
<p><code>Vue2.x</code>中的实现响应式的核心<code>API</code>：<code>Object.defineProperty</code></p>
<p><code>Vue3</code>响应式的核心<code>Proxy</code></p>
<h2 id="3、如何实现深度的监听"><a href="#3、如何实现深度的监听" class="headerlink" title="3、如何实现深度的监听"></a>3、如何实现深度的监听</h2><p>下面我们先来看一段代码：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//触发视图</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">updateView</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;视图更新&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//监听属性的变化</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">defineReactive</span>(<span class="params">target, key, value</span>) &#123;</span><br><span class="line">  <span class="title class_">Object</span>.<span class="title function_">defineProperty</span>(target, key, &#123;</span><br><span class="line">    <span class="title function_">get</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> value;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">set</span>(<span class="params">newValue</span>) &#123;</span><br><span class="line">      <span class="keyword">if</span> (newValue !== value) &#123;</span><br><span class="line">        <span class="comment">//设置新的值</span></span><br><span class="line">        <span class="comment">// 注意：value一直在闭包中，此处设置完之后，再get时也是会获取最新的值。</span></span><br><span class="line">        value = newValue;</span><br><span class="line">        <span class="title function_">updateView</span>();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">observer</span>(<span class="params">target</span>) &#123;</span><br><span class="line">  <span class="keyword">if</span> (<span class="keyword">typeof</span> target !== <span class="string">&quot;object&quot;</span> || target === <span class="literal">null</span>) &#123;</span><br><span class="line">    <span class="comment">//不是对象或者是数组</span></span><br><span class="line">    <span class="keyword">return</span> target;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">in</span> target) &#123;</span><br><span class="line">    <span class="title function_">defineReactive</span>(target, key, target[key]);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//准备数据</span></span><br><span class="line"><span class="keyword">const</span> data = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&quot;zhangsan&quot;</span>,</span><br><span class="line">  <span class="attr">age</span>: <span class="number">20</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//监听数据</span></span><br><span class="line"><span class="title function_">observer</span>(data);</span><br><span class="line">data.<span class="property">name</span> = <span class="string">&quot;lisi&quot;</span>;</span><br><span class="line">data.<span class="property">age</span> = <span class="number">21</span>;</span><br></pre></td></tr></table></figure>

<p>以上通过<code>Object.defineProperty</code>这个<code>api</code>,将<code>data</code>转换成响应式的了。</p>
<p>下面把<code>data</code>对象中的内容修改一下：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> data = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&quot;zhansan&quot;</span>,</span><br><span class="line">  <span class="attr">age</span>: <span class="string">&quot;21&quot;</span>,</span><br><span class="line">  <span class="attr">info</span>: &#123;</span><br><span class="line">    <span class="attr">address</span>: <span class="string">&quot;北京&quot;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line">data.<span class="property">info</span>.<span class="property">address</span> = <span class="string">&quot;上海&quot;</span>;</span><br></pre></td></tr></table></figure>

<p>在<code>data</code>中增加了一个<code>info</code>属性，该属性是一个对象，下面给该对象中的<code>address</code>属性赋值，但是视图没有进行更新。这里需要进行深度的监听。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//监听属性的变化</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">defineReactive</span>(<span class="params">target, key, value</span>) &#123;</span><br><span class="line">  <span class="title function_">observer</span>(value); <span class="comment">//深度监听</span></span><br><span class="line">  <span class="title class_">Object</span>.<span class="title function_">defineProperty</span>(target, key, &#123;</span><br><span class="line">    <span class="title function_">get</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> value;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">set</span>(<span class="params">newValue</span>) &#123;</span><br><span class="line">      <span class="keyword">if</span> (newValue !== value) &#123;</span><br><span class="line">        <span class="comment">//深度监听</span></span><br><span class="line">        <span class="title function_">observer</span>(newValue);</span><br><span class="line">        <span class="comment">//设置新的值</span></span><br><span class="line">        <span class="comment">// 注意：value一直在闭包中，此处设置完之后，再get时也是会获取最新的值。</span></span><br><span class="line">        value = newValue;</span><br><span class="line">        <span class="title function_">updateView</span>();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在调用<code> Object.defineProperty</code>之前进行了深度监听，同时在<code>set</code>方法的内部进行设置值也进行了深度的监听。</p>
<p>为什么在<code>set</code>方法的内部也要进行深度监听呢？</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">data.age=&#123;num:21&#125;</span><br><span class="line">data.age.num=22</span><br></pre></td></tr></table></figure>

<p>如果不在<code>set</code>方法的内部设置深度监听，以上的内容无法更新视图。</p>
<p><code>Object.defineProperty</code>的缺点：</p>
<p>第一：深度监听，需要递归到底，一次性计算量大。</p>
<p>第二：<code>data.x=&#39;200&#39;</code>这种新增的属性，监听不到，需要用到<code>Vue.set</code></p>
<p><code>delete data.name</code> 删除属性，监听不到，需要使用<code>Vue.delete</code></p>
<p>也就是说<code>Object.defineProperty</code>无法监听新增与删除属性.</p>
<h2 id="4、vue-如何监听数组的变化"><a href="#4、vue-如何监听数组的变化" class="headerlink" title="4、vue 如何监听数组的变化"></a>4、vue 如何监听数组的变化</h2><p>关于<code>Object.defineProperty</code>的缺点还有一条，无法原生的监听数组，需要特殊处理。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//重新定义数组原型</span></span><br><span class="line"><span class="keyword">const</span> oldArrayProperty = <span class="title class_">Array</span>.<span class="property"><span class="keyword">prototype</span></span>;</span><br><span class="line"><span class="comment">//创建新的对象，原型指向oldArrayProperty,在扩展新的方法不会影响到原型.</span></span><br><span class="line"><span class="keyword">const</span> arrProto = <span class="title class_">Object</span>.<span class="title function_">create</span>(oldArrayProperty);</span><br><span class="line">[<span class="string">&quot;push&quot;</span>, <span class="string">&quot;pop&quot;</span>, <span class="string">&quot;shift&quot;</span>, <span class="string">&quot;unshift&quot;</span>, <span class="string">&quot;splice&quot;</span>].<span class="title function_">forEach</span>(<span class="function">(<span class="params">methodName</span>) =&gt;</span> &#123;</span><br><span class="line">  arrProto[methodName] = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="title function_">undateView</span>(); <span class="comment">//更新视图</span></span><br><span class="line"></span><br><span class="line">    oldArrayProperty[methodName].<span class="title function_">call</span>(<span class="variable language_">this</span>, ...<span class="variable language_">arguments</span>);</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>创建了一个新的对象，原型指向了<code>oldArrayProperty</code>.这样在扩展新的方法的时候不会影响到原型。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//如果target是一个数组</span></span><br><span class="line"><span class="keyword">if</span> (<span class="title class_">Array</span>.<span class="title function_">isArray</span>(target)) &#123;</span><br><span class="line">  target.<span class="property">__proto__</span> = arrProto;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果<code>target</code>是一个数组，那么给原型赋值为<code>arrProto</code>.</p>
<p>这样，我们在使用<code>data.nums.push(80)</code>的时候，调用的是我们自己扩展的方法。</p>
<p>以上的操作相对来说比较麻烦，你可能有这样的想法，直接进行如下的修改：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">observer</span>(<span class="params">target</span>) &#123;</span><br><span class="line">  <span class="keyword">if</span> (<span class="keyword">typeof</span> target !== object || target === <span class="literal">null</span>) &#123;</span><br><span class="line">    <span class="comment">//不是对象或者是数组</span></span><br><span class="line">    <span class="keyword">return</span> target;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//如果target是一个数组</span></span><br><span class="line">  <span class="comment">// if(Array.isArray(target))&#123;</span></span><br><span class="line">  <span class="comment">//   target.__proto__=arrProto</span></span><br><span class="line">  <span class="comment">//&#125;</span></span><br><span class="line"></span><br><span class="line">  <span class="title class_">Array</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">push</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="title function_">updateView</span>();</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">in</span> target) &#123;</span><br><span class="line">    <span class="title function_">defineReactive</span>(target, key, target[key]);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在上面的代码中，我们直接修改了<code>Array</code>的原型的<code>push</code>方法中的代码，为其添加了更新视图的操作，这样不就很容易了吗？</p>
<p>但是，这样做是不行的。因为对全局的<code>Array</code>原型进行了污染。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//触发视图</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">updateView</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;视图更新&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//重新定义数组原型</span></span><br><span class="line"><span class="keyword">const</span> oldArrayProperty = <span class="title class_">Array</span>.<span class="property"><span class="keyword">prototype</span></span>;</span><br><span class="line"><span class="comment">//创建新的对象，原型指向oldArrayProperty,在扩展新的方法不会影响到原型.</span></span><br><span class="line"><span class="keyword">const</span> arrProto = <span class="title class_">Object</span>.<span class="title function_">create</span>(oldArrayProperty);</span><br><span class="line">[<span class="string">&quot;push&quot;</span>, <span class="string">&quot;pop&quot;</span>, <span class="string">&quot;shift&quot;</span>, <span class="string">&quot;unshift&quot;</span>, <span class="string">&quot;splice&quot;</span>].<span class="title function_">forEach</span>(<span class="function">(<span class="params">methodName</span>) =&gt;</span> &#123;</span><br><span class="line">  arrProto[methodName] = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="title function_">updateView</span>(); <span class="comment">//更新视图</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;this==&quot;</span>, <span class="variable language_">this</span>);</span><br><span class="line">    oldArrayProperty[methodName].<span class="title function_">call</span>(<span class="variable language_">this</span>, ...<span class="variable language_">arguments</span>);</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">//监听属性的变化</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">defineReactive</span>(<span class="params">target, key, value</span>) &#123;</span><br><span class="line">  <span class="title class_">Object</span>.<span class="title function_">defineProperty</span>(target, key, &#123;</span><br><span class="line">    <span class="title function_">get</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> value;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">set</span>(<span class="params">newValue</span>) &#123;</span><br><span class="line">      <span class="keyword">if</span> (newValue !== value) &#123;</span><br><span class="line">        <span class="comment">//设置新的值</span></span><br><span class="line">        <span class="comment">// 注意：value一直在闭包中，此处设置完之后，再get时也是会获取最新的值。</span></span><br><span class="line">        value = newValue;</span><br><span class="line">        <span class="title function_">updateView</span>();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">observer</span>(<span class="params">target</span>) &#123;</span><br><span class="line">  <span class="keyword">if</span> (<span class="keyword">typeof</span> target !== object || target === <span class="literal">null</span>) &#123;</span><br><span class="line">    <span class="comment">//不是对象或者是数组</span></span><br><span class="line">    <span class="keyword">return</span> target;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//如果target是一个数组</span></span><br><span class="line">  <span class="keyword">if</span> (<span class="title class_">Array</span>.<span class="title function_">isArray</span>(target)) &#123;</span><br><span class="line">    target.<span class="property">__proto__</span> = arrProto;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">in</span> target) &#123;</span><br><span class="line">    <span class="title function_">defineReactive</span>(target, key, target[key]);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//准备数据</span></span><br><span class="line"><span class="keyword">const</span> data = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&quot;zhangsan&quot;</span>,</span><br><span class="line">  <span class="attr">age</span>: <span class="number">20</span>,</span><br><span class="line">  <span class="attr">nums</span>: [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>],</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//监听数据</span></span><br><span class="line"><span class="title function_">observer</span>(data);</span><br><span class="line">data.<span class="property">nums</span>.<span class="title function_">push</span>(<span class="number">80</span>);</span><br></pre></td></tr></table></figure>

<h2 id="5、虚拟-DOM-Virtual-DOM-和-diff"><a href="#5、虚拟-DOM-Virtual-DOM-和-diff" class="headerlink" title="5、虚拟 DOM(Virtual DOM)和 diff"></a>5、虚拟 DOM(Virtual DOM)和 diff</h2><p><code>vdom</code>是实现<code>Vue</code>与<code>React</code>的重要基石</p>
<p><code>diff</code>算法是<code>vdom</code>中最核心，最关键的部分，也是面试的过程中经常会被问到的一个问题。</p>
<p>我们知道<code>DOM</code>操作非常耗费性能，在以前的时候，可以使用<code>jQuery</code>,来自行控制<code>DOM</code>操作的时机，但是现在使用的<code>Vue</code>和<code>React</code>是数据驱动视图，那么如何有效的控制<code>DOM</code>操作呢？</p>
<p><code>Vue</code>与<code>React</code>框架的内部通过虚拟<code>DOM</code>来完成对真实<code>DOM</code>的操作，也就是虚拟<code>DOM</code>就是用<code>js</code>模拟<code>DOM</code>结构，计算出最小的变更，然后在进行<code>DOM</code>的操作。</p>
<p>下面，我们看一段伪代码，是通过<code>JS</code>来模拟<code>DOM</code>结构的代码</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;div1&quot;</span> <span class="attr">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>vdom<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">ul</span> <span class="attr">style</span>=<span class="string">&quot;font-size:20px&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>a<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>对应的<code>js</code>模拟的<code>DOM</code>结构的代码如下：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="attr">tag</span>:<span class="string">&#x27;div&#x27;</span>,</span><br><span class="line">    <span class="attr">props</span>:&#123;</span><br><span class="line">        <span class="attr">className</span>:<span class="string">&#x27;container&#x27;</span>,</span><br><span class="line">         <span class="attr">id</span>:<span class="string">&#x27;div1&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="attr">children</span>:[</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">tag</span>:<span class="string">&#x27;p&#x27;</span>,</span><br><span class="line">            <span class="attr">children</span>:<span class="string">&#x27;vdom&#x27;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">tag</span>:<span class="string">&#x27;ul&#x27;</span>,</span><br><span class="line">            <span class="attr">props</span>:&#123;<span class="attr">style</span>:<span class="string">&#x27;font-size:20px&#x27;</span>&#125;</span><br><span class="line">            <span class="attr">children</span>:[</span><br><span class="line">            	<span class="attr">tag</span>:<span class="string">&#x27;li&#x27;</span>,</span><br><span class="line">                <span class="attr">children</span>:<span class="string">&#x27;a&#x27;</span></span><br><span class="line">            ]</span><br><span class="line">    		<span class="comment">//......</span></span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>通过前面的学习，我们也知道在<code>Vue</code>中是通过<code>snabbdom</code>这个开源库来实现<code>虚拟DOM与``diff</code>算法的.</p>
<h2 id="6、diff-算法"><a href="#6、diff-算法" class="headerlink" title="6、diff 算法"></a>6、diff 算法</h2><p>关于<code>diff</code>算法，在前面的课程中，我们已经重点讲解过。如果忘记了可以复习。</p>
<p>这里首先注意的就是如下几点内容：</p>
<p>第一：只比较同一层级，不跨级比较</p>
<p>第二：<code>tag</code>不相同，则直接删掉重建，不再深度比较。</p>
<p>第三：<code>tag</code>和<code>key</code>，两者都相同，则认为是相同节点，则不再深度比较。</p>
<h2 id="7、模板编译"><a href="#7、模板编译" class="headerlink" title="7、模板编译"></a>7、模板编译</h2><p>模板不是<code>html</code>,模板中是有指令，插值表达式，<code>js</code>表达式，能够实现循环，判断。而<code>html</code>只是标签语言，不能实现循环，判断，只有<code>js</code>才能实现循环判断，因此模板一定是转换为某种<code>js</code>的代码，这个过程叫做编译模板。</p>
<ul>
<li>模板编译为<code>render</code>函数，执行<code>render</code>函数返回<code>vnode</code></li>
<li>基于<code>vnode</code>再执行<code>patch</code>和<code>diff</code></li>
<li>如果使用<code>webpack vue-loader</code>,会在开发环境下编译模板。</li>
</ul>
<h2 id="8、初次渲染过程"><a href="#8、初次渲染过程" class="headerlink" title="8、初次渲染过程"></a>8、初次渲染过程</h2><ul>
<li>解析模板为<code>render</code>函数</li>
<li>触发响应式，监听<code>data</code>属性<code>getter/setter</code></li>
<li>执行<code>render</code>函数，生成<code>vnode</code>,执行<code>patch(elem,vnode)</code></li>
</ul>
<p>注意：在执行<code>render</code> 函数的时候会触发<code>getter</code>,例如，如下代码：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;p&gt;</span><br><span class="line">    &#123;&#123;message&#125;&#125;</span><br><span class="line">&lt;/p&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      message: &quot;hello world&quot;, //会触发get</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p>在上面的代码中，在模板中使用了<code>message</code>，所以会执行<code>get</code>操作。</p>
<p><strong>更新过程</strong></p>
<ul>
<li>修改<code>data</code>,触发<code>setter</code></li>
<li>重新执行<code>render</code>,生成<code>newVnode</code></li>
<li><code>patch(vnode,newVnode)</code>,比较<code>vnode</code>与<code>newVnode</code></li>
</ul>
<h2 id="9、异步渲染"><a href="#9、异步渲染" class="headerlink" title="9、异步渲染"></a>9、异步渲染</h2><p><code>Vue</code>组件的渲染是异步渲染，这里可以参考<code>$nextTick</code>.</p>
<p>汇总<code>data</code>的修改，一次性更新视图</p>
<p>减少<code>Dom</code>操作次数，提高性能。</p>
<h1 id="三、常见面试题"><a href="#三、常见面试题" class="headerlink" title="三、常见面试题"></a>三、常见面试题</h1><h2 id="1、为何在-v-for-中使用-key"><a href="#1、为何在-v-for-中使用-key" class="headerlink" title="1、为何在 v-for 中使用 key"></a>1、为何在 v-for 中使用 key</h2><p>在<code>v-for</code>中必须使用<code>key</code>,<code>key</code>的取值不能是<code>index</code>和<code>random</code>.</p>
<p>使用<code>key</code>的原因：</p>
<p>在<code>diff</code>算法中通过<code>tag</code>和<code>key</code>来判断，是否为<code>sameNode</code>,减少渲染次数，提升渲染的性能</p>
<h2 id="2、Vue-组件如何通讯"><a href="#2、Vue-组件如何通讯" class="headerlink" title="2、Vue 组件如何通讯"></a>2、Vue 组件如何通讯</h2><p>父子组件：<code>props</code>与<code>this.$emit</code></p>
<p>自定义事件<code>event.$on event.$emit</code></p>
<p>vuex</p>
<h2 id="3、双向数据绑定-v-model-的实现原理"><a href="#3、双向数据绑定-v-model-的实现原理" class="headerlink" title="3、双向数据绑定 v-model 的实现原理"></a>3、双向数据绑定 v-model 的实现原理</h2><p><code>input</code>元素的<code>value=this.msg</code></p>
<p>绑定<code>input</code>事件<code>this.msg=$event.target.value</code></p>
<p><code>data</code>更新触发<code>render</code></p>
<h2 id="4、computed-的特点"><a href="#4、computed-的特点" class="headerlink" title="4、computed 的特点"></a>4、computed 的特点</h2><p>缓存，<code>data</code>不变不会重新计算</p>
<p>提高性能</p>
<h2 id="5、为什么组件-data-必须是一个函数"><a href="#5、为什么组件-data-必须是一个函数" class="headerlink" title="5、为什么组件 data 必须是一个函数"></a>5、为什么组件 data 必须是一个函数</h2><p>组件就是一个类，我们去使用组件，实际上就是对组件的一个实例化，如果<code>data</code>不是一个函数的话，每个组件实例的<code>data</code>都是一样的，那这样就共享了。</p>
<h2 id="6、多个组件有相同的逻辑，如何抽离"><a href="#6、多个组件有相同的逻辑，如何抽离" class="headerlink" title="6、多个组件有相同的逻辑，如何抽离"></a>6、多个组件有相同的逻辑，如何抽离</h2><p>使用<code>mixin</code></p>
<p><code>mixin</code>有一些缺点</p>
<h2 id="7、何时使用异步组件"><a href="#7、何时使用异步组件" class="headerlink" title="7、何时使用异步组件"></a>7、何时使用异步组件</h2><p>加载大组件</p>
<p>路由异步加载</p>
<h2 id="8、何时需要使用-keep-alive"><a href="#8、何时需要使用-keep-alive" class="headerlink" title="8、何时需要使用 keep-alive"></a>8、何时需要使用 keep-alive</h2><p>缓存组件，不需要重复渲染</p>
<p>例如多个静态<code>tab</code>页的切换</p>
<p>性能优化</p>
<h2 id="9、何时需要使用-beforeDestory"><a href="#9、何时需要使用-beforeDestory" class="headerlink" title="9、何时需要使用 beforeDestory"></a>9、何时需要使用 beforeDestory</h2><p>解绑自定义事件<code>event.$off</code></p>
<p>清除定时器</p>
<p>解绑自定义的<code>DOM</code>事件（注意是自己绑定的事件，<code>VUe</code>绑定的事件会自行解除）</p>
<h2 id="10、Vuex-中-action-和-mutation-有何区别"><a href="#10、Vuex-中-action-和-mutation-有何区别" class="headerlink" title="10、Vuex 中 action 和 mutation 有何区别"></a>10、Vuex 中 action 和 mutation 有何区别</h2><p>action 中处理异步，mutation 不可以</p>
<p>mutation 做原子操作</p>
<p><code>action</code>可以整合多个<code>mutation</code></p>
<h2 id="11、Vue-router-常用的路由模式"><a href="#11、Vue-router-常用的路由模式" class="headerlink" title="11、Vue-router 常用的路由模式"></a>11、Vue-router 常用的路由模式</h2><p><code>hash</code>模式</p>
<p><code>H5 history</code>(需要服务端的支持)</p>
<p>两者的实现原理</p>
<h2 id="12、Vue-常见性能优化方式"><a href="#12、Vue-常见性能优化方式" class="headerlink" title="12、Vue 常见性能优化方式"></a>12、Vue 常见性能优化方式</h2><p>合理使用<code>v-show</code>与<code>v-if</code></p>
<p>合理用<code>computed</code></p>
<p><code>v-for</code>时加上<code>key</code></p>
<p>自定义事件,<code>dom</code>事件要及时的销毁</p>
<p>合理使用异步组件</p>
<p>合理使用<code>keep-alive</code></p>
<p><code>data</code>层级不要太深（递归次数多）</p>
<p><code>webpack</code>的优化</p>
<p>前端通用的性能优化，如图片懒加载</p>
<p>使用<code>ssr</code></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://example.com">Justic</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2022/07/15/Vue/Vue%20%E5%B8%B8%E8%A7%81%E9%9D%A2%E8%AF%95%E9%A2%98/Vue%E9%9D%A2%E8%AF%95%E9%A2%98/">http://example.com/2022/07/15/Vue/Vue 常见面试题/Vue面试题/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://justicc.top/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">Justic</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Vue/">Vue</a></div><div class="post_share"><div class="social-share" data-image="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/yj/wallhaven-yj1kgl.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/07/15/Vue/Vue%20%E8%99%9A%E6%8B%9Fdom/Vue%20%E8%99%9A%E6%8B%9Fdom/"><img class="prev-cover" src= "" data-lazy-src="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/k7/wallhaven-k7jmg6.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Vue 虚拟DOM</div></div></a></div><div class="next-post pull-right"><a href="/2022/07/15/Vue/Vuex%20%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86/Vuex%20%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86/"><img class="next-cover" src= "" data-lazy-src="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/m9/wallhaven-m9y289.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Vuex 状态管理</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/07/13/Vue/Vue%20Router/vueRouter/" title="Vue 路由"><img class="cover" src= "" data-lazy-src="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/ne/wallhaven-ne8mzl.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-07-13</div><div class="title">Vue 路由</div></div></a></div><div><a href="/2022/07/13/Vue/Vue%20%E5%93%8D%E5%BA%94%E5%BC%8F%E5%8E%9F%E7%90%86/Vue%E5%93%8D%E5%BA%94%E5%BC%8F%E5%8E%9F%E7%90%86/" title="Vue 响应式原理"><img class="cover" src= "" data-lazy-src="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/j3/wallhaven-j3qq15.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-07-13</div><div class="title">Vue 响应式原理</div></div></a></div><div><a href="/2022/10/20/Vue/Vue%20%E5%93%8D%E5%BA%94%E5%BC%8F%E8%A7%A3%E6%9E%90/Vue%E5%93%8D%E5%BA%94%E5%BC%8F%E8%A7%A3%E6%9E%90/" title="Vue 响应式解析"><img class="cover" src= "" data-lazy-src="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/z8/wallhaven-z82evv.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-10-20</div><div class="title">Vue 响应式解析</div></div></a></div><div><a href="/2022/07/15/Vue/Vue%20%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%B8%B2%E6%9F%93/Vue%E6%9C%8D%E5%8A%A1%E7%AB%AF%E6%B8%B2%E6%9F%93/" title="Vue 服务器渲染"><img class="cover" src= "" data-lazy-src="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/ne/wallhaven-ne8mzl.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-07-15</div><div class="title">Vue 服务器渲染</div></div></a></div><div><a href="/2022/07/15/Vue/Vue%20%E6%BA%90%E7%A0%81%E8%A7%A3%E8%AF%BB/Vue%E6%BA%90%E7%A0%81%E8%A7%A3%E8%AF%BB/" title="Vue 源码解读"><img class="cover" src= "" data-lazy-src="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/j3/wallhaven-j3qq15.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-07-15</div><div class="title">Vue 源码解读</div></div></a></div><div><a href="/2022/07/15/Vue/Vue%20%E8%99%9A%E6%8B%9Fdom/Vue%20%E8%99%9A%E6%8B%9Fdom/" title="Vue 虚拟DOM"><img class="cover" src= "" data-lazy-src="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/k7/wallhaven-k7jmg6.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-07-15</div><div class="title">Vue 虚拟DOM</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="https://portrait.gitee.com/uploads/avatars/user/2982/8948190_JIAXInT_1624177607.png!avatar200" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Justic</div><div class="author-info__description">最是人间留不住 朱颜辞镜花辞树</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">22</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">2</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80%E3%80%81Vue-%E9%9D%A2%E8%AF%95%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86"><span class="toc-text">一、Vue 面试基础知识</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81computed-%E5%92%8C-watch"><span class="toc-text">1、computed 和 watch</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81v-if-%E4%B8%8E-v-show"><span class="toc-text">2、v-if 与 v-show</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81%E5%BE%AA%E7%8E%AF%E5%88%97%E8%A1%A8"><span class="toc-text">3、循环列表</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81%E7%88%B6%E5%AD%90%E7%BB%84%E4%BB%B6%E5%A6%82%E4%BD%95%E9%80%9A%E4%BF%A1"><span class="toc-text">4、父子组件如何通信</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81%E5%85%84%E5%BC%9F%E7%BB%84%E4%BB%B6%E4%B9%8B%E9%97%B4%E7%9A%84%E9%80%9A%E4%BF%A1"><span class="toc-text">5、兄弟组件之间的通信</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6%E3%80%81%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-text">6、生命周期</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7%E3%80%81%E8%87%AA%E5%AE%9A%E4%B9%89-v-model"><span class="toc-text">7、自定义 v-model</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8%E3%80%81-nextTick"><span class="toc-text">8、$nextTick</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9%E3%80%81slot"><span class="toc-text">9、slot</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10%E3%80%81%E5%8A%A8%E6%80%81%E7%BB%84%E4%BB%B6"><span class="toc-text">10、动态组件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11%E3%80%81%E5%BC%82%E6%AD%A5%E5%8A%A0%E8%BD%BD%E7%BB%84%E4%BB%B6"><span class="toc-text">11、异步加载组件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12%E3%80%81keep-alive"><span class="toc-text">12、keep-alive</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#13%E3%80%81mixin"><span class="toc-text">13、mixin</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E3%80%81Vue-%E5%8E%9F%E7%90%86"><span class="toc-text">二、Vue 原理</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81MVVM"><span class="toc-text">1、MVVM</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E7%9B%91%E5%90%AC-data-%E5%8F%98%E5%8C%96%E7%9A%84%E6%A0%B8%E5%BF%83-API-%E6%98%AF%E4%BB%80%E4%B9%88"><span class="toc-text">2、监听 data 变化的核心 API 是什么</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E6%B7%B1%E5%BA%A6%E7%9A%84%E7%9B%91%E5%90%AC"><span class="toc-text">3、如何实现深度的监听</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81vue-%E5%A6%82%E4%BD%95%E7%9B%91%E5%90%AC%E6%95%B0%E7%BB%84%E7%9A%84%E5%8F%98%E5%8C%96"><span class="toc-text">4、vue 如何监听数组的变化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81%E8%99%9A%E6%8B%9F-DOM-Virtual-DOM-%E5%92%8C-diff"><span class="toc-text">5、虚拟 DOM(Virtual DOM)和 diff</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6%E3%80%81diff-%E7%AE%97%E6%B3%95"><span class="toc-text">6、diff 算法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7%E3%80%81%E6%A8%A1%E6%9D%BF%E7%BC%96%E8%AF%91"><span class="toc-text">7、模板编译</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8%E3%80%81%E5%88%9D%E6%AC%A1%E6%B8%B2%E6%9F%93%E8%BF%87%E7%A8%8B"><span class="toc-text">8、初次渲染过程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9%E3%80%81%E5%BC%82%E6%AD%A5%E6%B8%B2%E6%9F%93"><span class="toc-text">9、异步渲染</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%B8%B8%E8%A7%81%E9%9D%A2%E8%AF%95%E9%A2%98"><span class="toc-text">三、常见面试题</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81%E4%B8%BA%E4%BD%95%E5%9C%A8-v-for-%E4%B8%AD%E4%BD%BF%E7%94%A8-key"><span class="toc-text">1、为何在 v-for 中使用 key</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81Vue-%E7%BB%84%E4%BB%B6%E5%A6%82%E4%BD%95%E9%80%9A%E8%AE%AF"><span class="toc-text">2、Vue 组件如何通讯</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81%E5%8F%8C%E5%90%91%E6%95%B0%E6%8D%AE%E7%BB%91%E5%AE%9A-v-model-%E7%9A%84%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86"><span class="toc-text">3、双向数据绑定 v-model 的实现原理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81computed-%E7%9A%84%E7%89%B9%E7%82%B9"><span class="toc-text">4、computed 的特点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81%E4%B8%BA%E4%BB%80%E4%B9%88%E7%BB%84%E4%BB%B6-data-%E5%BF%85%E9%A1%BB%E6%98%AF%E4%B8%80%E4%B8%AA%E5%87%BD%E6%95%B0"><span class="toc-text">5、为什么组件 data 必须是一个函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6%E3%80%81%E5%A4%9A%E4%B8%AA%E7%BB%84%E4%BB%B6%E6%9C%89%E7%9B%B8%E5%90%8C%E7%9A%84%E9%80%BB%E8%BE%91%EF%BC%8C%E5%A6%82%E4%BD%95%E6%8A%BD%E7%A6%BB"><span class="toc-text">6、多个组件有相同的逻辑，如何抽离</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7%E3%80%81%E4%BD%95%E6%97%B6%E4%BD%BF%E7%94%A8%E5%BC%82%E6%AD%A5%E7%BB%84%E4%BB%B6"><span class="toc-text">7、何时使用异步组件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8%E3%80%81%E4%BD%95%E6%97%B6%E9%9C%80%E8%A6%81%E4%BD%BF%E7%94%A8-keep-alive"><span class="toc-text">8、何时需要使用 keep-alive</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9%E3%80%81%E4%BD%95%E6%97%B6%E9%9C%80%E8%A6%81%E4%BD%BF%E7%94%A8-beforeDestory"><span class="toc-text">9、何时需要使用 beforeDestory</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10%E3%80%81Vuex-%E4%B8%AD-action-%E5%92%8C-mutation-%E6%9C%89%E4%BD%95%E5%8C%BA%E5%88%AB"><span class="toc-text">10、Vuex 中 action 和 mutation 有何区别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11%E3%80%81Vue-router-%E5%B8%B8%E7%94%A8%E7%9A%84%E8%B7%AF%E7%94%B1%E6%A8%A1%E5%BC%8F"><span class="toc-text">11、Vue-router 常用的路由模式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12%E3%80%81Vue-%E5%B8%B8%E8%A7%81%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E6%96%B9%E5%BC%8F"><span class="toc-text">12、Vue 常见性能优化方式</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/11/09/JavaScript/ES6%E6%96%B0%E7%89%B9%E6%80%A7/index/" title="ES6新特性"><img src= "" data-lazy-src="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/m9/wallhaven-m9y289.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="ES6新特性"/></a><div class="content"><a class="title" href="/2022/11/09/JavaScript/ES6%E6%96%B0%E7%89%B9%E6%80%A7/index/" title="ES6新特性">ES6新特性</a><time datetime="2022-11-09T12:55:33.000Z" title="发表于 2022-11-09 20:55:33">2022-11-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/10/20/Vue/Vue%20%E5%93%8D%E5%BA%94%E5%BC%8F%E8%A7%A3%E6%9E%90/Vue%E5%93%8D%E5%BA%94%E5%BC%8F%E8%A7%A3%E6%9E%90/" title="Vue 响应式解析"><img src= "" data-lazy-src="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/z8/wallhaven-z82evv.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue 响应式解析"/></a><div class="content"><a class="title" href="/2022/10/20/Vue/Vue%20%E5%93%8D%E5%BA%94%E5%BC%8F%E8%A7%A3%E6%9E%90/Vue%E5%93%8D%E5%BA%94%E5%BC%8F%E8%A7%A3%E6%9E%90/" title="Vue 响应式解析">Vue 响应式解析</a><time datetime="2022-10-20T12:05:37.000Z" title="发表于 2022-10-20 20:05:37">2022-10-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/08/08/JavaScript/JavaScript%E5%8E%9F%E5%9E%8B%E5%92%8C%E5%8E%9F%E5%9E%8B%E9%93%BE/index/" title="JavaScript原型和原型链"><img src= "" data-lazy-src="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/z8/wallhaven-z82evv.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JavaScript原型和原型链"/></a><div class="content"><a class="title" href="/2022/08/08/JavaScript/JavaScript%E5%8E%9F%E5%9E%8B%E5%92%8C%E5%8E%9F%E5%9E%8B%E9%93%BE/index/" title="JavaScript原型和原型链">JavaScript原型和原型链</a><time datetime="2022-08-08T09:08:31.000Z" title="发表于 2022-08-08 17:08:31">2022-08-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/08/08/JavaScript/JavaScript%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%A2%9E%E5%BC%BA%E7%9F%A5%E8%AF%86/index/" title="JavaScript对象的增强知识"><img src= "" data-lazy-src="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/j3/wallhaven-j3qq15.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JavaScript对象的增强知识"/></a><div class="content"><a class="title" href="/2022/08/08/JavaScript/JavaScript%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%A2%9E%E5%BC%BA%E7%9F%A5%E8%AF%86/index/" title="JavaScript对象的增强知识">JavaScript对象的增强知识</a><time datetime="2022-08-08T07:43:36.000Z" title="发表于 2022-08-08 15:43:36">2022-08-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/08/08/JavaScript/JavaScript%E5%87%BD%E6%95%B0%E5%A2%9E%E5%BC%BA%E7%9F%A5%E8%AF%86/index/" title="JavaScript函数增强知识"><img src= "" data-lazy-src="https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/28/wallhaven-2881yx.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JavaScript函数增强知识"/></a><div class="content"><a class="title" href="/2022/08/08/JavaScript/JavaScript%E5%87%BD%E6%95%B0%E5%A2%9E%E5%BC%BA%E7%9F%A5%E8%AF%86/index/" title="JavaScript函数增强知识">JavaScript函数增强知识</a><time datetime="2022-08-08T03:25:40.000Z" title="发表于 2022-08-08 11:25:40">2022-08-08</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://dogefs.s3.ladydaily.com/~/source/wallhaven/full/yj/wallhaven-yj1kgl.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By Justic</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a><img src= "" data-lazy-src="https://static.dy208.cn/o_1dfilp8ruo521thr1hvf18ji17soa.png">
<a href="http://www.beian.miit.gov.cn/"  style="color:#f72b07" target="_blank">晋ICP备2021014440号-2</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="chat_btn" type="button" title="聊天"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'KgIigedIwh0GobYsWWfPMEdO-gzGzoHsz',
      appKey: 'WpJEQP22asTau19i2tq1yQhU',
      avatar: 'justic',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><div class="aplayer no-destroy" data-id="5076309057" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="true" data-lrcType="-1"> </div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-nest.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = true;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-show-text.min.js" data-mobile="true" data-text="富强,民主,文明,和谐,平等,公正,法治,爱国,敬业,诚信,友善" data-fontsize="15px" data-random="true" async="async"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>
  function butterfly_clock_anzhiyu_injector_config(){
    var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
    var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img class="entered loading" id="card-clock-loading" src= "" data-lazy-src="https://cdn.cbd.int/hexo-butterfly-clock-anzhiyu/lib/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading"/></div></div></div></div></div>';
    console.log('已挂载butterfly_clock_anzhiyu')
    parent_div_git.insertAdjacentHTML("afterbegin",item_html)
    }
  var elist = 'null'.split(',');
  var cpage = location.pathname;
  var epage = 'all';
  var qweather_key = 'f854dfdd82d747fca7fd04f092861e87';
  var gaud_map_key = '427416c518f4476a01117be40057a892';
  var baidu_ak_key = 'undefined';
  var flag = 0;
  var clock_rectangle = '112.982279,28.19409';
  var clock_default_rectangle_enable = 'false';

  for (var i=0;i<elist.length;i++){
    if (cpage.includes(elist[i])){
      flag++;
    }
  }

  if ((epage ==='all')&&(flag == 0)){
    butterfly_clock_anzhiyu_injector_config();
  }
  else if (epage === cpage){
    butterfly_clock_anzhiyu_injector_config();
  }
  </script><script src="https://widget.qweather.net/simple/static/js/he-simple-common.js?v=2.0"></script><script data-pjax src="https://cdn.cbd.int/hexo-butterfly-clock-anzhiyu/lib/clock.min.js"></script><!-- hexo injector body_end end --></body></html>